package cn.webestar.scms.user.svcapi;

import cn.webestar.scms.commons.Assert;
import cn.webestar.scms.commons.R;
import cn.webestar.scms.commons.SysCode;
import cn.webestar.scms.commons.api.PageVO;
import cn.webestar.scms.microsvc.controller.BaseController;
import cn.webestar.scms.user.api.DepartmentApi;
import cn.webestar.scms.user.api.dto.Department.*;
import cn.webestar.scms.user.entity.Department;
import cn.webestar.scms.user.service.DepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 部门
 */
@RestController
@RequestMapping("/svcapi/Department")
public class DepartmentSvcController extends BaseController<Department, Long> implements DepartmentApi {

    @Autowired
    private DepartmentService service;

    @Override
    public IService<Department> getService() {
        return service;
    }

    @Override
    @PostMapping("/delete")
    public R<Boolean> delete(@RequestParam("id") Long id) {
        return super._delete(id);
    }

    @Override
    @PostMapping("/findById")
    public R<Department> findById(@RequestParam("id") Long id) {
        return super._findById(id);
    }

    @Override
    @PostMapping("/findByIds")
    public R<Map<Long, Department>> findByIds(@RequestBody Set<Long> ids) {
        return super._findByIds(ids);
    }

    @Override
    @PostMapping("/create")
    public R<Department> create(@RequestBody DepartmentCreateDTO body) {
        return super._create(body);
    }

    @Override
    @PostMapping("/update")
    public R<Department> update(@RequestBody DepartmentUpdateDTO body) {
        return super._update(body);
    }

    @Override
    @PostMapping("/page")
    public R<PageVO<Department>> page(@RequestBody DepartmentPageDTO body) {
        return super._page(body);
    }

    @Override
    @PostMapping("/findOne")
    public R<Department> findOne(@RequestBody DepartmentSimpleDTO body) {
        return super._findOne(body);
    }

    @Override
    @PostMapping("/limitList")
    public R<List<Department>> limitList(@RequestBody DepartmentLimitDTO body) {
        return super._limitList(body);
    }

    @Override
    @PostMapping("/pageRoot")
    public R<PageVO<Department>> pageRoot(@RequestBody DepartmentRootPageDTO body) {
        LambdaQueryWrapper<Department> queryWrapper = body.toQueryWapper();
        queryWrapper.isNull(Department::getPid);
        IPage<Department> page = getService().page(new Page<>(body.getPageNum(), body.getPageSize()), queryWrapper);
        return R.success(new PageVO<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords()));
    }

    @Override
    @PostMapping("/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody Set<Long> ids) {
        QueryWrapper<Department> qw = new QueryWrapper<>();
        qw.in("pid", ids);
        qw.eq("status", 1);
        long count = service.count(qw);
        Assert.isTrue(count == 0, SysCode.biz(1), "部门下存在子部门，请先删除子部门");
        boolean b = service.removeByIds(ids);
        return R.success(b);
    }

    @Override
    @PostMapping("/save")
    public R<Department> save(@RequestBody DepartmentUpdateDTO body) {
        Department o = body.toDO();
        if(body.getId()==null){
            getService().save(o);
        } else {
            getService().updateById(o);
        }
        return R.success(o);
    }

}
